#include "iic.h"

static void I2C_Delay(void)
{
	uint8_t i;
	for (i = 0; i < 10; i++);
}

void IIC_Init(void)
{
  GPIO_InitType GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE);
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pins  =PIN_I2C_SCL | PIN_I2C_SDA;;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_OD;
  GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  GPIO_Init(PORT_I2C_SCL, &GPIO_InitStructure);
  I2C_SCL_HIGH;                                  
  I2C_SDA_HIGH;
}
void IIC_Start()
{
	 I2C_SCL_HIGH ;
	 I2C_SDA_HIGH ;
	 I2C_Delay();
	 I2C_SDA_LOW;
	 I2C_Delay();
	 I2C_SCL_LOW;
	 I2C_Delay();
}

void IIC_Stop()
{
	I2C_SCL_LOW;
	I2C_SDA_LOW;
	I2C_SCL_HIGH ;
	I2C_Delay();
	I2C_SDA_HIGH ;
	I2C_Delay();
}

void IIC_Write_Byte(uchar IIC_Byte)
{
 	unsigned char i;
	for(i=0;i<8;i++)		
	{
		if(IIC_Byte & 0x80)
		I2C_SDA_HIGH;
		else
		I2C_SDA_LOW;
		I2C_Delay();
		I2C_SCL_HIGH;
		I2C_Delay();
		I2C_SCL_LOW;
		IIC_Byte<<=1;
		I2C_Delay();
	}
	I2C_SDA_HIGH;
	I2C_SCL_HIGH ;
	I2C_Delay();
	I2C_SCL_LOW;
	I2C_Delay();
}

